﻿using GetSlaughterItem.API.Application.Commands.Week02.BasicSettings;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.leos
{
    /// <summary>
    /// 销售明细反填
    /// </summary>
    public class SaleMangeHandler : IRequestHandler<SaleMangeCommend, Result>
    {
        private readonly IBaseRepositoryGet<SalesDetails> SalesDetails;
        private readonly IBaseRepositoryGet<Clients> Clients;

        public SaleMangeHandler(IBaseRepositoryGet<SalesDetails> SalesDetails, IBaseRepositoryGet<Clients> Clients)
        {
            this.SalesDetails = SalesDetails;
            this.Clients = Clients;
        }

        public async Task<Result> Handle(SaleMangeCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {

                var list = from a in SalesDetails.GetAll()
                            join b in Clients.GetAll() on a.CustomerId equals b.CustomerId
                            where a.SalesDetailsId == request.SalesDetailsId
                            select new
                            {
                                a.SalesDetailsId,
                                a.SalesDetailsDate,
                                a.SalesDetailsOrderNumber,
                                a.SalesDetailsOrderNum,
                                a.SalesDetailsState,
                                b.CustomerId,
                                b.CustomerNickname,
                            };

                var info = list.FirstOrDefault();

                if (info != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = info;
                    result.Msg = "反填成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
